// pages/weather/weather.js
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk = new QQMapWX({
  key: '6ILBZ-BCMCR-27ZWX-WDKS7-7NFHV-ROF24' 
});  
Page({

    /**
     * 页面的初始数据
     */
    data: {
      //天气相关信息
    weatherNow:[],
    weather3D:[],
    weather7D:[],
    updateTime:'',
    //经纬度
    latitude:'',
    longitude:'',
    //城市名字
    cityName:[],
    //年月日信息
    yearinfo:[],
    //搜索框获取的城市信息
    searchcitylist:[],
    //搜索时页面是否展示，默认展示页面，搜索时隐藏页面
    isShowResultView:true,
    searchvalue:'',
    // 天气指数信息
    wearthIndex:[]
    },
    //得到用户经纬度信息
    getuserlocation(){
      var that = this
    wx.getLocation({
      success(res){
        // console.log(1)
        // if(res.latitude== wx.setStorageSync('latitude', res.latitude)&&
        // res.longitude==wx.setStorageSync('longitude', res.longitude)){
        // }
      that.setData({
          latitude:res.latitude,
          longitude:res.longitude
      })
      wx.setStorageSync('latitude', res.latitude)
      wx.setStorageSync('longitude', res.longitude)
    }
  })
  },
    // 获取当天天气
    getWeatherNow(){
      var that=this
      var la=wx.getStorageSync('latitude')
      var lo=wx.getStorageSync('longitude')
      wx.request({
        url: 'http://localhost:8089/weather/getnow',
        data:{
          latitude:la,
          longitude:lo,
        },
        success(res){
            // console.log(res)
            that.setData({
              weatherNow:res.data
            })
        }
      })
    },

    // 获取未来三天天气
    getWeather3D(){
      var that=this
      var la=wx.getStorageSync('latitude')
      var lo=wx.getStorageSync('longitude')
      // console.log(la)
      wx.request({
        url: 'http://localhost:8089/weather/get3D',
        data:{
          latitude:la,
          longitude:lo,
        },
        success(res){
            // console.log(res)
            that.setData({
              weather3D:res.data.dailyList,
              updateTime:res.data.updateTime
              
            })
        }
      })
    },
    // 获取未来7天天气
    getWeather7D(){
      var that=this
      var la=wx.getStorageSync('latitude')
      var lo=wx.getStorageSync('longitude')
      // console.log(la)
      wx.request({
        url: 'http://localhost:8089/weather/get7D',
        data:{
          latitude:la,
          longitude:lo,
        },
        success(res){
            console.log(res)
            that.setData({
              weather7D:res.data,
              updateTime:res.data.updateTime
            })
        }
      })
    },
    //获取城市信息
    getcityInfo(){
      var la=wx.getStorageSync('latitude')
      var lo=wx.getStorageSync('longitude')
      var that=this
      qqmapsdk.reverseGeocoder({
      location:{
        latitude: la,
          longitude: lo
      },
      success(res){
      // console.log(res)
      that.setData({
        cityName:res.result.address_component.district
      })
      }
    })
    },
    //得到当前年月日
    getNowDate(){
      var that=this
      wx.request({
        url: 'http://localhost:8089/weather/getnowdate',
        success(res){
          // console.log(res)
          that.setData({
            yearinfo:res.data
          })
        }
      })
    },
    
    //天气指数信息
    getWeatherIndexInfo(){
      var that=this
      var la=wx.getStorageSync('latitude')
      var lo=wx.getStorageSync('longitude')
      wx.request({
        url: 'http://localhost:8089/weather/getweatherindex',
        data:{
          latitude: la,
            longitude: lo
        },
        success(res){
          // console.log(res)
        that.setData({
        wearthIndex:res.data
        })
        }
      })
    },
    
    //搜索框输入内容变化时
    searchchange(e){
    var location=e.detail
    var that=this
    wx.request({
      url: 'http://localhost:8089/weather/getsearchlist',
      data:{
        location:location
      },
      success(res){
      console.log(res)
      if(res.data==''){
        that.setData({
          isShowResultView:true,
          searchcitylist:res.data
        })
      }
      else{
      that.setData({
        searchcitylist:res.data,
        isShowResultView:false
      })
    }
      }
    })
    },
    

    // 点击搜索结果获取坐标信息
    getsearchcityinfo(e){
    console.log(e.currentTarget.dataset)
    wx.setStorageSync('latitude', e.currentTarget.dataset.lat)
    wx.setStorageSync('longitude', e.currentTarget.dataset.lon)
    this.setData({
      isShowResultView:true,
      searchcitylist:'',
      searchvalue:''
    })
    // this.onShow()
    this.onLoad()
    },


    /** 
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    this.getWeatherNow()
    this.getWeather3D()
    this.getWeather7D()
    this.getcityInfo()
    this.getNowDate()
    this.getWeatherIndexInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
      this.getuserlocation()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})